package com.ky.manage.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ky.manage.R;
import com.ky.manage.constant.OverallData;
import com.ky.manage.model.BaseEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Fragment基类
 */
public abstract class BaseFragment extends Fragment {
    //获取TAG的fragment名称
    protected final String TAG = this.getClass().getSimpleName();
    public Context mContext;
    private View mRootView;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.mContext = context;
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layoutId(),container,false);
        this.mRootView = view;

        initView(view);
        initData();
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSubscribe(BaseEvent event) {

    }

    public <V extends TextView> V findTextView(int id, String text, View.OnClickListener listener) {
        V v = findView(id);
        v.setText(text);
        v.setOnClickListener(listener);
        return v;
    }

    public <V extends TextView> V findTextView(int id, String text) {
        return findTextView(id, text, null);
    }

    /**通过id查找并获取控件，使用时不需要强转
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public <V extends View> V findView(int id) {
        return (V) mRootView.findViewById(id);
    }
    /**通过id查找并获取控件，并setOnClickListener
     * @param id
     * @param l
     * @return
     */
    public <V extends View> V findView(int id, View.OnClickListener l) {
        V v = findView(id);
        v.setOnClickListener(l);
        return v;
    }
    /**通过id查找并获取控件，并setOnClickListener
     * @param id
     * @param l
     * @return
     */
    public <V extends View> V findViewById(int id, View.OnClickListener l) {
        return findView(id, l);
    }

    /**
     * 初始化布局
     * @return 布局id
     */
    protected abstract int layoutId();

    /**
     * 初始化控件
     * @param rootView 布局view
     */
    protected abstract void initView(View rootView);

    /**
     * 初始化，绑定数据
     */
    protected abstract void initData();

    //启动新Activity方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**打开新的Activity，向左滑入效果
     * @param intent
     */
    public void toActivity(Intent intent) {
        toActivity(intent, true);
    }
    /**打开新的Activity
     * @param intent
     * @param showAnimation
     */
    public void toActivity(Intent intent, boolean showAnimation) {
        toActivity(intent, -1, showAnimation);
    }

    /**打开新的Activity，向左滑入效果
     * @param intent
     * @param requestCode
     */
    public void toActivity(Intent intent, int requestCode) {
        toActivity(intent, requestCode, true);
    }

    /**打开新的Activity
     * @param intent
     * @param requestCode
     * @param showAnimation
     */
    public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) {
        OverallData.hd.post(()->{
            if (intent == null) {
                Log.w(TAG, "toActivity  intent == null >> return;");
                return;
            }
            //fragment中使用context.startActivity会导致在fragment中不能正常接收onActivityResult
            Activity curActivity = getActivity();
            if (requestCode < 0) {
                curActivity.startActivity(intent);
            } else {
                curActivity.startActivityForResult(intent, requestCode);
            }
            if (showAnimation) {
                curActivity.overridePendingTransition(R.anim.right_push_in, R.anim.hold);
            } else {
                curActivity.overridePendingTransition(R.anim.null_anim, R.anim.null_anim);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
